package com.cskaoyan.service;

import com.cskaoyan.exception.CustomException;
import com.cskaoyan.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    AccountMapper accountMapper;

    public void transfer(Integer fromId, Integer destId, Integer money) throws CustomException {
        Integer fromMoney = accountMapper.selectMoneyById(fromId);
        Integer destMoney = accountMapper.selectMoneyById(destId);

        fromMoney -= money;
        destMoney += money;

        accountMapper.update(fromId, fromMoney);
        //int i = 1 / 0;
        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        if (true) {
            throw new CustomException();
        }
        accountMapper.update(destId, destMoney);
    }
}
